//
//  KDSettingViewController.m
//  警用舆情平台
//
//  Created by 魏强 on 15/5/12.
//  Copyright (c) 2015年 魏强. All rights reserved.
//

#import "KDSettingViewController.h"
#import "AFNetworking.h"
#import "AFHTTPRequestOperation.h"
#import "KDLiaisonProfessionController.h"
#import "GBCollectViewController.h"
#import "GBAboutUsViewController.h"
#import "GBNoticeSetViewController.h"
#import "GBModifyPassWordViewController.h"
#import "KDNewfeatureController.h"
#import "KDLiaisonProfessionController.h"
#import "KDHttpTool.h"
#define baseUrl2 @"&method=loginOut&m=mobile&token="
//#define NoEndUrl @"http://ql.yunyq.cn/MobileAction.do?method=unreadwqtaskcnt&token="

@interface KDSettingViewController () <UITableViewDataSource,UITableViewDelegate>
{
    //5.12改
    NSMutableArray *photoArray; //照片名称的数组
    NSMutableArray *titleArray; //标题名称的数组
    UITableView *tableView1;
    UIButton *quitBtn; //退出按钮4
    NSString *logOutUrl; //退出登录接口
    
    //6.26
    NSTimer *timer_; //无限循环的时间控制器
    UIButton *showCountBtn; // 显示未读个数的btn
    
    // 2016/3/15
    UIButton *backButton;
}

@end

@implementation KDSettingViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的设置";
    self.view.backgroundColor = WQColor(235, 235, 235);
    photoArray = [NSMutableArray array];
    titleArray = [NSMutableArray array];
    
    KDAccount *accout = [KDAccountTool account];
    NSString *userName = [NSString stringWithFormat:@"用户名：%@",accout.userName];
    //KDLog(@"%@",accout.userName);
    
    NSArray *array1 = @[userName,@"关于我们"];
    NSArray *array3 = @[@"用户名1",@"关于我们1"];
    
    [titleArray addObject:array1];
    [photoArray addObject:array3];
    
    backButton = [UIButton buttonWithType:0];
    backButton.frame = CGRectMake(0, 0, 30, 30);
    [backButton setImage:[UIImage imageNamed:@"leftArrow"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backTheView)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    //初始化tableview
    [self createTableView];
    //创建label将tableview图片下的横线补上
    [self createLabelUnder];
    //初始化退出按钮
    [self createQuitBtn];
    // Do any additional setup after loading the view.
}

#pragma mark - 返回主界面
-(void)backTheView
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 创建label将tableview图片下的横线补上
-(void)createLabelUnder
{

}

#pragma mark - 初始化退出按钮
-(void)createQuitBtn
{
    quitBtn = [UIButton buttonWithType:0];
    quitBtn.frame = CGRectMake(15, CGRectGetMaxY(tableView1.frame) + 50, screenW - 30, 32);
    [quitBtn setTitle:@"退出" forState:UIControlStateNormal];
    quitBtn.layer.cornerRadius = 5;
    [quitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    quitBtn.backgroundColor = KDHeadColor;
    [quitBtn addTarget:self action:@selector(quitLogin) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:quitBtn];
}

#pragma mark - 初始化tableview
-(void)createTableView
{
    //设置tableview2
    tableView1 = [[UITableView alloc] initWithFrame:CGRectMake(0, -45,screenW, 170) style:UITableViewStyleGrouped];
    //加一个段尾 去掉多余的横线
    tableView1.tableFooterView = [[UIView alloc] init];
    tableView1.dataSource = self;
    tableView1.delegate = self;
    //设置tableview不能滚动
    tableView1.scrollEnabled = NO;
    [self.view addSubview:tableView1];
    
    showCountBtn =[UIButton buttonWithType:0];
    showCountBtn.frame = CGRectMake(-10000, 0, 20, 20);
    showCountBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    showCountBtn.layer.cornerRadius = 10.0f;
    showCountBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    showCountBtn.backgroundColor = [UIColor redColor];
    [showCountBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [tableView1 addSubview:showCountBtn];
}
#pragma mark - 退出登录
-(void)quitLogin
{
//    NSString *str = [NSString stringWithFormat:@"http://itunes.apple.com/cn/lookup?id=1070824095"];
//    [KDHttpTool get:str parameters:nil success:^(id responseObj) {
//        NSLog(@"%@", responseObj);
//    } failure:^(NSError *error) {
//        NSLog(@"%@", error);
//    }];
    [self createLogOut];
}

#pragma mark - 退出登录请求url
-(void)createLogOut
{
    KDAccount *account = [KDAccountTool account];
    logOutUrl = [NSString stringWithFormat:@"%@%@%@", ServerActionAddr, baseUrl2,account.tokens];
    //KDLog(@"%@",logOutUrl);
    //请求数据
    NSURLRequest *httpReq = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:logOutUrl]];
    
    AFHTTPRequestOperation *reqOper = [[AFHTTPRequestOperation alloc] initWithRequest:httpReq];
    [reqOper setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *op,id success){
        NSHTTPURLResponse *response = [op response];
        NSInteger code = [response statusCode];
        if (code == 200)
        {
            NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:[op responseData] options:NSJSONReadingMutableContainers error:nil];
            if([jsonDict[@"status"] isEqualToString:@"ok"])
            {
                
                //KDLog(@"退出登录成功");
                KDNewfeatureController *login = [[KDNewfeatureController alloc] init];
                //跳转到登陆界面
                //设置切换风格
                login.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
                //2.切换控制器
                [self presentViewController:login animated:YES completion:nil];
            }
            //账号在别处登录
            else if([jsonDict[@"status"] isEqualToString:@"notlogin"])
            {
                NSString *_message = @"该账号已在其他地方登陆";
                UIAlertView *promptAlert = [[UIAlertView alloc] initWithTitle:@"提示:" message:_message delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
                [NSTimer scheduledTimerWithTimeInterval:1.8f
                                                 target:self
                                               selector:@selector(timerFireMethod:)
                                               userInfo:promptAlert
                                                repeats:YES];
                [promptAlert show];
                
                KDNewfeatureController *login = [[KDNewfeatureController alloc] init];
                //跳转到登陆界面
                //设置切换风格
                login.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
                //2.切换控制器
                [self presentViewController:login animated:YES completion:nil];
            }
        }
    } failure:^(AFHTTPRequestOperation *op, NSError *error) {
        if (error) {
            KDLog(@"req error %@",error);
        }
    }];
    [reqOper start];
}

#pragma mark - 弹出的对话框内容
- (void)timerFireMethod:(NSTimer*)theTimer//弹出框
{
    UIAlertView *promptAlert = (UIAlertView*)[theTimer userInfo];
    [promptAlert dismissWithClickedButtonIndex:0 animated:NO];
    promptAlert = nil;
}

#pragma mark - 分段数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
{
    return [titleArray count];
}

#pragma mark - tableview返回行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[titleArray objectAtIndex:section] count];
}

#pragma mark 设置分组标题内容高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.5;
}

#pragma mark - 返回行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

#pragma mark - 段头内容
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"";
}

#pragma mark - 显示tableview
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:@"cell"];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    for(int i = 1; i < 2; i++)
    {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, i * 60 - 5, screenW, 10)];
        view.backgroundColor = WQColor(235, 235, 235);
        [cell.contentView addSubview:view];
    }
    //KDLog(@"1");
    NSArray *ar = [titleArray objectAtIndex:indexPath.section];
    cell.textLabel.text = [ar objectAtIndex:indexPath.row];
    
    NSArray *br = [photoArray objectAtIndex:indexPath.section];
    cell.imageView.image = [UIImage imageNamed:[br objectAtIndex:indexPath.row]];
    cell.textLabel.font = [UIFont systemFontOfSize:15.5];
    cell.textLabel.textColor = WQColor(64, 64, 64);
    [cell setSeparatorInset:UIEdgeInsetsZero];
    if(indexPath.section == 0)
    {
        if(indexPath.row == 0)
        {
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        else
        {
            cell.selectionStyle = UITableViewCellSelectionStyleDefault;
            //定制cell右按钮
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
    }
    else
    {
        cell.selectionStyle = UITableViewCellSelectionStyleDefault;
        //定制cell右按钮
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    return cell;
}

#pragma mark - 当单元格被选中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];//返回时 取消选中
    
    if(indexPath.section == 0)
    {
//        //跳转收藏界面
//        if (indexPath.row == 1)
//        {
//            GBCollectViewController *collect = [[GBCollectViewController alloc] init];
//            UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"设置" style:UIBarButtonItemStylePlain target:nil action:nil];
//            [self.navigationItem setBackBarButtonItem:backItem];
//            self.hidesBottomBarWhenPushed = YES;
//            [self.navigationController pushViewController:collect animated:YES];
//            self.hidesBottomBarWhenPushed = NO;
//        }
//        //跳转到我的上报
//        else if(indexPath.row == 2)
//        {
//            KDLiaisonProfessionController *report = [[KDLiaisonProfessionController alloc] init];
//            UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"设置" style:UIBarButtonItemStylePlain target:nil action:nil];
//            [self.navigationItem setBackBarButtonItem:backItem];
//            self.hidesBottomBarWhenPushed = YES;
//            [self.navigationController pushViewController:report animated:YES];
//            self.hidesBottomBarWhenPushed = NO;
//        }
        //跳转到关于我们
        if(indexPath.row == 1)
        {
            GBAboutUsViewController *aboutUs = [[GBAboutUsViewController alloc] init];
            UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"设置" style:UIBarButtonItemStylePlain target:nil action:nil];
            [self.navigationItem setBackBarButtonItem:backItem];
            self.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:aboutUs animated:YES];
            self.hidesBottomBarWhenPushed = NO;
        }
    }
//    else if(indexPath.section == 1)
//    {
//        //跳转到通知设置
//        if(indexPath.row == 0)
//        {
//            GBNoticeSetViewController *noticeSetup = [[GBNoticeSetViewController alloc] init];
//            UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"设置" style:UIBarButtonItemStylePlain target:nil action:nil];
//            [self.navigationItem setBackBarButtonItem:backItem];
//            self.hidesBottomBarWhenPushed = YES;
//            [self.navigationController pushViewController:noticeSetup animated:YES];
//            self.hidesBottomBarWhenPushed = NO;
//        }
//        //跳转到关于我们
//        else if(indexPath.row == 1)
//        {
//            GBAboutUsViewController *aboutUs = [[GBAboutUsViewController alloc] init];
//            UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"设置" style:UIBarButtonItemStylePlain target:nil action:nil];
//            [self.navigationItem setBackBarButtonItem:backItem];
//            self.hidesBottomBarWhenPushed = YES;
//            [self.navigationController pushViewController:aboutUs animated:YES];
//            self.hidesBottomBarWhenPushed = NO;
//        }
//    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end